#!/bin/sh

echo Setting up the new machine
echo Scripted by raingc@gmail.com

# Install required packages
echo Installing packages
apt-get update
apt-get upgrade -y
apt-get install python lighttpd ssh sudo vim rsync munin munin-node openvpn iptables gcc g++ make git gitweb zip unzip php5-cgi php5-sqlite php5-gd -y
apt-get update
apt-get upgrade -y

# Set up gitweb
# TODO: Replace this by SCM-manager
echo Setting up gitweb
mkdir /var/www/git
cp /usr/share/gitweb/* /var/www/git/

# Set up Munin
echo Setting up Munin
echo htmldir /var/www/monitoring >> /etc/munin/munin.conf
echo contact.someuser.command mail -s \"VPS Notification\" raingc@gmail.com >> /etc/munin/munin.conf
mkdir /var/www/monitoring
echo \<HEAD\>\<meta HTTP-EQUIV=\"REFRESH\" content=\"0\; url=http://SERVERADDR/monitoring/localdomain/localhost.localdomain/\"\>\</HEAD\> > /var/www/monitoring/index.html
chown -R munin:adm /var/www/monitoring
/etc/init.d/munin-node restart

# Set up lighttpd
echo Setting up lighttpd
echo server.modules = \(\"mod_fastcgi\", \"mod_access\", \"mod_alias\", \"mod_compress\", \"mod_redirect\", \"mod_cgi\"\) > /etc/lighttpd/lighttpd.conf
echo server.document-root        = \"/var/www\" >> /etc/lighttpd/lighttpd.conf
echo server.upload-dirs          = \( \"/var/cache/lighttpd/uploads\" \) >> /etc/lighttpd/lighttpd.conf
echo server.errorlog             = \"/var/log/lighttpd/error.log\" >> /etc/lighttpd/lighttpd.conf
echo server.pid-file             = \"/var/run/lighttpd.pid\" >> /etc/lighttpd/lighttpd.conf
echo server.username             = \"www-data\" >> /etc/lighttpd/lighttpd.conf
echo server.groupname            = \"www-data\" >> /etc/lighttpd/lighttpd.conf
echo cgi.assign = \(\".cgi\" =\> \"\"\) >> /etc/lighttpd/lighttpd.conf
echo fastcgi.server = \(\".php\" =\> \(\(\"bin-path\" =\> \"/usr/bin/php5-cgi\", \"socket\" =\> \"/tmp/php.socket\"\)\)\) >> /etc/lighttpd/lighttpd.conf
echo index-file.names            = \( \"index.php\", \"index.html\", \"index.htm\", \"default.htm\", \"index.lighttpd.html\", \"index.cgi\"\) >> /etc/lighttpd/lighttpd.conf
echo url.access-deny             = \( \"~\", \".inc\" \) >> /etc/lighttpd/lighttpd.conf
echo static-file.exclude-extensions = \( \".php\", \".pl\", \".fcgi\" \) >> /etc/lighttpd/lighttpd.conf
echo include_shell \"/usr/share/lighttpd/use-ipv6.pl\" >> /etc/lighttpd/lighttpd.conf
echo dir-listing.encoding        = \"utf-8\" >> /etc/lighttpd/lighttpd.conf
echo server.dir-listing          = \"enable\" >> /etc/lighttpd/lighttpd.conf
echo compress.cache-dir          = \"/var/cache/lighttpd/compress/\" >> /etc/lighttpd/lighttpd.conf
echo compress.filetype           = \( \"application/x-javascript\", \"text/css\", \"text/html\", \"text/plain\" \) >> /etc/lighttpd/lighttpd.conf
echo include_shell \"/usr/share/lighttpd/create-mime.assign.pl\" >> /etc/lighttpd/lighttpd.conf
echo include_shell \"/usr/share/lighttpd/include-conf-enabled.pl\" >> /etc/lighttpd/lighttpd.conf
/etc/init.d/lighttpd restart

# Set up user
echo Adding user raing. When prompted, enter password
useradd raing
passwd raing
mkdir /home/raing
mkdir /home/raing/.ssh
echo ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMFXu9Oyj3WJVToppTyCpZqk1aJvbyORoOAFKJn7geBGmKsYSBpTFVh42clBsRn/F4y4nmyEMZcqrJrnNPK6EGNzkKEOVsxiIkqFp5rMkKijF7QWbzaMaEYBi0hkyKgiBhSXwa1aDAmQwFBdl7Hy413iiH/BRY55ZcTyY0dUuihNHXIkL1M3cHbMvhvmbsEKloHCUuL54ty1pi17SiSm/uLQS3cHb0mAmrVEWnXRHc+zw3QkjJueI88dMMIBflJLfGrDHNlK8yuF7UH0+6K3cNWP3ZDENAAByl9sfIKTTgFu6/icvfkjD54Rw5Wtmb5DeFVvIfG9e+3wCTyRO85++b RainG@RainG-PC >> /home/raing/.ssh/authorized_keys

# Set up ssh
echo Configuring SSH
echo PasswordAuthentication no >> /etc/ssh/sshd_config
echo AllowUsers raing >> /etc/ssh/sshd_config
echo PermitRootLogin no >> /etc/ssh/sshd_config
echo PermitTunnel yes >> /etc/ssh/sshd_config
echo AllowTcpForwarding yes >> /etc/ssh/sshd_config

# Generate keys and local hoop ssh
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa 
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

# Set up sudo
echo Configuring SUDOers
echo raing ALL=\(ALL\) ALL >> /etc/sudoers

# Set up default shell and coloring
echo Changing bash as default shell
chsh -s /bin/bash root 
chsh -s /bin/bash raing

echo Changing ls with colors
echo alias ls=\"ls --color\" >> /home/raing/.bashrc
echo if [ -f ~/.bashrc ] >> /home/raing/.bash_profile
echo then >> /home/raing/.bash_profile
echo . ~/.bashrc >> /home/raing/.bash_profile
echo fi >> /home/raing/.bash_profile

echo alias ls=\"ls --color\" >> ~/.bashrc
echo if [ -f ~/.bashrc ] >> ~/.bash_profile
echo then >> ~/.bash_profile
echo . ~/.bashrc >> ~/.bash_profile
echo fi >> ~/.bash_profile

# Set up VPN
# TODO: Modify the key generation to make it fully automatic...
echo Setup VPN. Press enter when needed.
cp -R /usr/share/doc/openvpn/examples/easy-rsa /etc/openvpn
cd /etc/openvpn/easy-rsa/2.0
. ./vars     
./clean-all     
./build-ca 
./build-key-server server
./build-key client1
./build-dh
# TODO: Port forwarding and DNS service are specific to box. Set separately...
echo Setting up port-forwarding
echo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o venet0 -j SNAT --to SERVERADDR > /etc/rc.local
echo exit 0 >> /etc/rc.local
chmod 755 /etc/rc.local

echo dev tun >> /etc/openvpn/openvpn.conf
echo proto tcp >> /etc/openvpn/openvpn.conf
echo port 1723 >> /etc/openvpn/openvpn.conf
echo ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt >> /etc/openvpn/openvpn.conf
echo cert /etc/openvpn/easy-rsa/2.0/keys/server.crt >> /etc/openvpn/openvpn.conf
echo key /etc/openvpn/easy-rsa/2.0/keys/server.key >> /etc/openvpn/openvpn.conf
echo dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem >> /etc/openvpn/openvpn.conf
echo user nobody >> /etc/openvpn/openvpn.conf
echo group nogroup >> /etc/openvpn/openvpn.conf
echo server 10.8.0.0 255.255.255.0 >> /etc/openvpn/openvpn.conf
echo persist-key >> /etc/openvpn/openvpn.conf
echo persist-tun >> /etc/openvpn/openvpn.conf
echo verb 3 >> /etc/openvpn/openvpn.conf
echo client-to-client >> /etc/openvpn/openvpn.conf
echo push \"redirect-gateway def1\" >> /etc/openvpn/openvpn.conf
echo push \"dhcp-option DNS 8.8.8.8\" >> /etc/openvpn/openvpn.conf
echo push \"dhcp-option DNS 4.2.2.4\" >> /etc/openvpn/openvpn.conf
echo comp-lzo >> /etc/openvpn/openvpn.conf

echo Copying the keys to raing homespace
cp /etc/openvpn/easy-rsa/2.0/keys/ca.crt /home/raing/
cp /etc/openvpn/easy-rsa/2.0/keys/client1.crt /home/raing/
cp /etc/openvpn/easy-rsa/2.0/keys/client1.key /home/raing/

echo Creating client config file
echo client > /home/raing/client.ovpn
echo dev tun >> /home/raing/client.ovpn
echo proto tcp >> /home/raing/client.ovpn
echo remote SERVER_ADDR 1723 >> /home/raing/client.ovpn
echo resolv-retry infinite >> /home/raing/client.ovpn
echo nobind >> /home/raing/client.ovpn
echo persist-key >> /home/raing/client.ovpn
echo persist-tun >> /home/raing/client.ovpn
echo ca ca.crt >> /home/raing/client.ovpn
echo cert client1.crt >> /home/raing/client.ovpn
echo key client1.key >> /home/raing/client.ovpn
echo comp-lzo >> /home/raing/client.ovpn
echo verb 3 >> /home/raing/client.ovpn

echo Starting Service
/etc/init.d/openvpn start
/etc/rc.local &

# Lastly.. modify owner
echo Modifying Owner of raing\'s home directory
chown -R raing:raing /home/raing
